﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net;

namespace FrameWork.Design
{
    /// <summary>
    /// 表示获取网络适配器列表的转换器
    /// </summary>
    public class NetworkInterfaceConverter : TypeConverter
    {

        /// <summary>
        /// 获取该类型转换器是否支持标准值集
        /// </summary>
        /// <param name="context">当前类型所处的上下文</param>
        /// <returns>获取到的标准值集</returns>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// 获取类型转换器的所有标准值集
        /// </summary>
        /// <param name="context">当前类型所处于的上下文</param>
        /// <returns>获取到的标准值集</returns>
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            List<string> NicList = new List<string>();
            NetworkInterface[] NicLists = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface Adapter in NicLists)
            {
                NicList.Add(Adapter.Description);
            }
            return new StandardValuesCollection(NicList);
        }

        /// <summary>
        /// 根据描述获取网络适配器
        /// </summary>
        /// <param name="AdapterDes">网络适配器的描述</param>
        /// <returns>获取到的网络适配器实例</returns>
        /// <exception cref="System.Exception">未获取到具有指定描述的网络适配器</exception>
        public static NetworkInterface GetAdapter(string AdapterDes)
        {
            NetworkInterface[] NicLists = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface Adapter in NicLists)
            {
                if (Adapter.Description == AdapterDes)
                {
                    return Adapter;
                }
            }
            throw new Exception("未找到描述为 " + AdapterDes + " 的网络适配器！");
        }

        /// <summary>
        /// 获取指定网络适配器的第一条IpV4单播地址
        /// </summary>
        /// <param name="Adapter">要获取地址的网络适配器</param>
        /// <returns>获取到的IpV4地址</returns>
        /// <exception cref="System.Exception">未获取到有效的IpV4地址</exception>
        public static IPAddress GetIpV4Address(NetworkInterface Adapter)
        {
            if(Adapter.NetworkInterfaceType==NetworkInterfaceType.Loopback)
            {
                return IPAddress.Loopback;
            }
            IPInterfaceProperties Properties = Adapter.GetIPProperties();
            foreach (IPAddressInformation info in Properties.AnycastAddresses)
            {
                if(info.Address.AddressFamily==AddressFamily.InterNetwork)
                {
                    return info.Address;
                }
            }
            throw new Exception("未在描述为 " + Adapter.Description + " 的网卡上找到有效的IPV4单播地址");
        }

        /// <summary>
        /// 获取指定网络适配器的第一条IpV4广播地址
        /// </summary>
        /// <param name="Adapter">要获取地址的网络适配器</param>
        /// <returns>获取到的IpV4地址</returns>
        /// <exception cref="System.Exception">未获取到有效的IpV4地址</exception>
        public static IPAddress GetMulticastIpV4Address(NetworkInterface Adapter)
        {
            if(Adapter.NetworkInterfaceType==NetworkInterfaceType.Loopback)
            {
                return IPAddress.Broadcast;
            }
            foreach(IPAddressInformation info in Adapter.GetIPProperties().MulticastAddresses)
            {
                if(info.Address.AddressFamily==AddressFamily.InterNetwork)
                {
                    return info.Address;
                }
            }
            throw new Exception("未在描述为 " + Adapter.Description + " 的网卡上找到有效的IPV4广播地址");
        }

    }

}
